home *** CD-ROM | disk | FTP | other *** search
- /*
- * CBLibrary - FednetComp
- * Copyright (C) 2003 Chris Bazley
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
-
- /* Veneer to Fednet compression modules */
-
- /* ANSI library headers */
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
-
- /* RISC OS library headers */
- #include "kernel.h"
- #include "flex.h"
-
- /* Other headers */
- #include "Macros.h"
- #include "msgtrans.h"
- #include "hourglass.h"
- #include "timer.h"
- #include "FednetComp.h"
-
- extern _kernel_oserror shared_err_block;
-
- /* ----------------------------------------------------------------------- */
- /* Public functions */
-
- _kernel_oserror *load_compressed(char *filepath, flex_ptr buffer_anchor)
- {
- /* Allocate buffer and load compressed Fednet datafile */
-
- _kernel_last_oserror(); /* reset SCL's error recording */
-
- /* Get (decompressed) memory requirements */
- FILE *readfile = fopen(filepath, "r");
- if(readfile == NULL) {
- THROW(_kernel_last_oserror()) /* any OS error? */
- WRITE_ERR_SUB1(shared_err_block, "OpenInFail", filepath);
- return &shared_err_block; /* fail */
- }
-
- int buffer_size;
- if(fread(&buffer_size, sizeof(int), 1, readfile) != 1) {
- fclose(readfile);
- THROW(_kernel_last_oserror()) /* any OS error? */
- WRITE_ERR_SUB1(shared_err_block, "ReadFail", filepath);
- return &shared_err_block; /* fail */
- }
- fclose(readfile);
-
- /* Allocate buffer for data */
- if(!flex_alloc(buffer_anchor, buffer_size)) {
- WRITE_GERR(shared_err_block, "NoMem");
- return &shared_err_block; /* fail */
- }
-
- /* Construct CLI command */
- #ifndef OLD_SCL_STUBS
- char command[6+strlen(filepath)+2+8+1];
- #else
- char *command = malloc(6+strlen(filepath)+2+8+1);
- if(command == NULL) {
- WRITE_GERR(shared_err_block, "NoMem");
- return &shared_err_block; /* fail */
- }
- #endif
-
- int old_budge = flex_set_budge(0); /* prevent budge */
-
- #ifndef OLD_SCL_STUBS
- snprintf(command, sizeof(command), "Cload %s &%X", filepath, (int)*buffer_anchor);
- #else
- sprintf(command, "Cload %s &%X", filepath, (int)*buffer_anchor);
- #endif
-
- /* Decompress file */
- hourglass_on();
- int err = _kernel_oscli(command);
- hourglass_off();
-
- #ifdef OLD_SCL_STUBS
- free(command);
- #endif
-
- flex_set_budge(old_budge); /* allow budge */
-
- if(err == _kernel_ERROR) {
- flex_free(buffer_anchor);
- return _kernel_last_oserror(); /* fail */
- }
- return NULL; /* success */
- }
-
- /* ----------------------------------------------------------------------- */
-
- _kernel_oserror *save_compressed(char *filepath, int filetype, flex_ptr buffer_anchor)
- {
- /* Save the specified memory as a compressed Fednet file */
-
- /* Construct CLI command */
- {
- #ifndef OLD_SCL_STUBS
- char command[6+strlen(filepath)+2+8+2+8+1];
- #else
- char *command = malloc(6+strlen(filepath)+2+8+2+8+1);
- if(command == NULL) {
- WRITE_GERR(shared_err_block, "NoMem");
- return &shared_err_block; /* fail */
- }
- #endif
-
- int old_budge = flex_set_budge(0); /* prevent budge */
-
- #ifndef OLD_SCL_STUBS
- snprintf(command, sizeof(command), "CSave %s &%X &%X", filepath, (int)*buffer_anchor, ((int)*buffer_anchor + flex_size(buffer_anchor)));
- #else
- sprintf(command, "CSave %s &%X &%X", filepath, (int)*buffer_anchor, ((int)*buffer_anchor + flex_size(buffer_anchor)));
- #endif
-
- /* Compress file */
- hourglass_on();
- int err = _kernel_oscli(command);
- hourglass_off();
-
- #ifdef OLD_SCL_STUBS
- free(command);
- #endif
-
- flex_set_budge(old_budge); /* allow budge */
- if(err == _kernel_ERROR)
- return _kernel_last_oserror(); /* fail */
- }
-
- /* Set file type */
- {
- _kernel_osfile_block inout;
- inout.load = filetype;
- if(_kernel_osfile(18, filepath, &inout) == _kernel_ERROR)
- return _kernel_last_oserror(); /* fail */
- }
-
- return NULL; /* success */
- }
-